﻿using Max.Ph5_2009A.NewShopping.Domain;
using Max.Ph5_2009A.NewShopping.DTO;
using Max.Ph5_2009A.NewShopping.IRespository;
using Max.Ph5_2009A.NewShopping.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Max.Ph5_2009A.NewShopping.Service
{
    public class SystemCityDataService : BaseService<SystemCityDataModel>, ISystemCityDataService
    {
        ISystemCityDataRespository _respository;
        public SystemCityDataService(ISystemCityDataRespository respository, IUnitOfWork unitOfWork)
            : base(respository, unitOfWork)
        {
            _respository = respository;
        }

        /// <summary>
        /// 根据父ID获取数据(用于下拉框联动)
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        public IQueryable<SystemCityDataModel> GetCityData(int pid)
        {
            var list = _respository.Select().Where(x => x.PId == pid);
            return list;
        }

        /// <summary>
        /// 递归查询数据（用于级联选择器）
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<SystemCityDataRecursionOutputDto> GetResumateCityData(List<SystemCityDataModel> data, int id)
        {
            var list = data.Where(x => x.PId == id);

            List<SystemCityDataRecursionOutputDto> dtos = new();

            list.ToList().ForEach(p =>
            {
                var _t = GetResumateCityData(data, p.Id);
                dtos.Add(new SystemCityDataRecursionOutputDto
                {
                    Id = p.Id,
                    Name = p.Name,
                    PId = p.PId,
                    Sort = p.Sort,
                    Childrens = (_t != null && _t.Count > 0) ? _t : null

                });
            });
            return dtos;
        }
    }
}
